package com.iotp.utils.filter;

import com.iotp.entity.base.CodeEnum;
import com.iotp.entity.user.CommUser;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //验证的url
        String[] notFilter = new String[] {"main"};
        String urlPath = request.getServletPath();
        Boolean flg = false;
        for (String url : notFilter) {
            if ((urlPath.contains(url))) {
                flg = true;
            }
        }
        if(flg){
            CommUser session_user = (CommUser) request.getSession().getAttribute(CodeEnum.当前用户.msg);
            if(session_user == null || StringUtils.isEmpty(session_user.getItCode())){
                request.getRequestDispatcher("/session").forward(request, response);
            }else{
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
